rm(list = ls())
load("dataBJPOLS.RData")

pdf(file = "./Figures/Figure_A03_BJPOLS.pdf", height = 4, width = 9)
par(mfrow = c(1, 3))

inst.1 <- "judgeiv_hd"
endo.1 <- "pti"
outc.1 <- "vote_post"

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(severity)"
demo.controls <- "age + I(age^2) +  as.factor(race4) + female + vote_pre + as.factor(noteli) + regis_before"
case.controls <- "as.factor(any_drug) +  as.factor(any_weapon) +  as.factor(any_prop) + as.factor(any_prior_case)"

form.2 <- formula(paste(inst.1, "~", time.controls))
mod1b <- lm(form.2, data = last.cases)
last.cases$judgeiv_res <- last.cases$judgeiv - predict(mod1b, last.cases)

plot(density(last.cases$judgeiv_res[last.cases$race4 == "B" & last.cases$race_match == 1]), 
     main = "Black Defendants",
     xlab = "",
     cex.lab = 1, cex.main = 1.25, lwd = 2,
     col = "red", las = 1, ylim = c(0, 6))
lines(density(last.cases$judgeiv_res[last.cases$race4 == "B" & last.cases$race_match == 0]), 
      col = "blue", lwd = 2, lty = 2)

plot(density(last.cases$judgeiv_res[last.cases$race4 == "H" & last.cases$race_match == 1]), 
     main = "Hispanic Defendants",
     xlab = "Leave-out-case Residualized\n Pretrial Detention Rate",
     cex.lab = 1, cex.main = 1.25, lwd = 2,
     col = "red", las = 1, ylim = c(0, 6))
lines(density(last.cases$judgeiv_res[last.cases$race4 == "H" & last.cases$race_match == 0]), 
      col = "blue", lwd = 2, lty = 2)
legend(0.35, 6, c("Same \n race\n judge\n", "Different \n race\n judge"), col = c("blue", "red"),
       lty = c(2, 1), pch = c(NA, NA),
       box.lty = 0)

plot(density(last.cases$judgeiv_res[last.cases$race4 == "W" & last.cases$race_match == 1]), 
     main = "White Defendants",
     xlab = "",
     cex.lab = 1, cex.main = 1.25, lwd = 2,
     col = "red", las = 1, ylim = c(0, 6))
lines(density(last.cases$judgeiv_res[last.cases$race4 == "W" & last.cases$race_match == 0]), 
      col = "blue", lwd = 2, lty = 2)
dev.off()

